package test.lyj.ui;

import java.util.ArrayList;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyTab extends View {
	/**
	 * 下面页码背景颜色
	 */
	public static final int TAB_BG_COLOR1=0xFF464646;
	public static final int TAB_BG_COLOR2=0xFF232323;
	public static final int TAB_BG_COLOR3=0xFF232323;
	public static final int TAB_BG_COLOR4=0xFF000000;
	private Paint mPaint;// 

	private Paint mActiveTextPaint;// 
	private Paint mInactiveTextPaint;// 
	private ArrayList<MyTabMember> mTabMembers;// 
	private int chooseTabId;
	private OnTabClickListener mOnTabClickListener = null;

	public MyTab(Context context, AttributeSet attrs) {//attrs 就是XML中定义的标签
		super(context, attrs);
		mTabMembers = new ArrayList<MyTabMember>();

		mPaint = new Paint();//  
		mActiveTextPaint = new Paint();//画点击的文字用的画笔
		mInactiveTextPaint = new Paint();

		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(0xFFFFFF00);
		// FFFFFF00
		mPaint.setAntiAlias(true);

		mActiveTextPaint.setTextAlign(Align.CENTER);
		mActiveTextPaint.setTextSize(12);
		mActiveTextPaint.setColor(0xFFFFFFFF);// 点击以后的文字颜色
		// FFFFFFFF
		mActiveTextPaint.setAntiAlias(true);

		mInactiveTextPaint.setTextAlign(Align.CENTER);
		mInactiveTextPaint.setTextSize(12);
		mInactiveTextPaint.setColor(0xFF999999);// 通常状态下的文字颜色
		mInactiveTextPaint.setAntiAlias(true);
		chooseTabId = 0;
	}

	@Override
	protected void onDraw(Canvas canvas) {//每个View载入该时候会先调用构造函数,然后调用onDraw方法

		super.onDraw(canvas);
		int color1 = 0xFF000000;
		// int color1=0xFF004D85;
		Rect r = new Rect();
		this.getDrawingRect(r);
		//  
		int singleTabWidth = r.right
				/ (mTabMembers.size() != 0 ? mTabMembers.size() : 1);
		canvas.drawColor(color1);		
		Paint mPaint1=new Paint();		
		mPaint1.setShader( new LinearGradient(r.width()/2, 0, r.width()/2, r.height()/2,TAB_BG_COLOR1, TAB_BG_COLOR2, TileMode.MIRROR ) );
		canvas.drawRect( 0, 0, r.width(), r.height()/2, mPaint1 );
		Paint mPaint2=new Paint();
		mPaint2.setShader( new LinearGradient(r.width()/2, r.height()/2, r.width()/2, r.height(),TAB_BG_COLOR3, TAB_BG_COLOR4, TileMode.MIRROR ) );
		canvas.drawRect( 0, r.height()/2, r.width(), r.height(), mPaint2 );
		 
		Paint mPaint3=new Paint();
		mPaint3.setColor(0xFF434343);//
		canvas.drawLine(r.left, r.top + 1, r.right, r.top + 1, mPaint3);
		//  
		for (int i = 0; i < mTabMembers.size(); i++) {
			MyTabMember tabMember = mTabMembers.get(i);
			Bitmap icon = BitmapFactory.decodeResource(getResources(),
					tabMember.getIconResourceId());
			Bitmap iconColored = Bitmap.createBitmap(icon.getWidth(), icon
					.getHeight(), Bitmap.Config.ARGB_8888);
			//逻辑是:先有一个空白的图片,然后新一个画布,把图标画在画布上,然后把画布

			Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
					| Paint.FILTER_BITMAP_FLAG);
			Canvas iconCanvas = new Canvas();
			iconCanvas.setBitmap(iconColored);

			if (chooseTabId == i)// 
			{
				p.setShader(new BitmapShader(icon, TileMode.CLAMP,
						TileMode.CLAMP));

				// p.setShader( new LinearGradient(0, 0, icon.getWidth(),
				// icon.getHeight(),0xFFFFFFFF, 0xFF54C7E1, TileMode.CLAMP ) );
				// 选中时图片颜色渐变

			} else {

				p.setShader(new BitmapShader(icon, TileMode.CLAMP,
						TileMode.CLAMP));
				ColorMatrix cm = new ColorMatrix();
				cm.set(new float[] { 0.3f, 0.6f, 0.08f, 0, 0, 0.3f, 0.6f,
						0.08f, 0, 0, 0.3f, 0.6f, 0.08f, 0, 0, 0, 0, 0, 1, 0 });
				p.setColorFilter(new ColorMatrixColorFilter(cm));
				// p.setShader( new LinearGradient( 0, 0, icon.getWidth(),
				// icon.getHeight(),
				// 0xFFA2A2A2, 0xFF5F5F5F, TileMode.CLAMP) );

				// TileMode.CLAMP
				// p.setShader( new LinearGradient( 0, 0, icon.getWidth(),
				// icon.getHeight(),
				// 0xFF333333, 0xFF004D85, TileMode.CLAMP ) );

				// 图片未选中颜色
			}

			iconCanvas.drawRect(0, 0, icon.getWidth(), icon.getHeight(), p);
			for (int x = 0; x < icon.getWidth(); x++) {

				for (int y = 0; y < icon.getHeight(); y++) {

					if ((icon.getPixel(x, y) & color1) == 0) {//原来图片背景是透明的,现在改成白色

						iconColored.setPixel(x, y, 0x000000);
						// 图形按钮空隙背景颜色
					}

				}
			}

			//  
			int tabImgX = singleTabWidth * i
					+ (singleTabWidth / 2 - icon.getWidth() / 2);
			// 
			if (chooseTabId == i) {
				mPaint.setARGB(37, 255, 255, 255);// 选中时，背景颜色。
				// mPaint.setARGB( 137, 55, 255, 255 );
				// r.bottom-2背景距底部距离
				canvas.drawRoundRect(new RectF(r.left + singleTabWidth * i + 3,
						r.top + 3, r.left + singleTabWidth * (i + 1) - 3,
						r.bottom), 5, 5, mPaint);

				canvas.drawBitmap(iconColored, tabImgX, r.top + 5, null);
				// r.bottom-8文字与底部距离
				canvas.drawText(tabMember.getText(), singleTabWidth * i
						+ (singleTabWidth / 2), r.bottom - 4, mActiveTextPaint);

			} else {
				canvas.drawBitmap(iconColored, tabImgX, r.top + 5, null);
				canvas.drawText(tabMember.getText(), singleTabWidth * i
						+ (singleTabWidth / 2), r.bottom - 4,
						mInactiveTextPaint);

			}

		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent motionEvent) {
		// TODO Auto-generated method stub
		Rect r = new Rect();
		this.getDrawingRect(r);
		float singleTabWidth = r.right
				/ (mTabMembers.size() != 0 ? mTabMembers.size() : 1);
		int pressedTab = (int) ((motionEvent.getX() / singleTabWidth) - (motionEvent
				.getX() / singleTabWidth) % 1);
		chooseTabId = pressedTab;
		if (this.mOnTabClickListener != null) {

			this.mOnTabClickListener.onTabClick(mTabMembers.get(pressedTab)
					.getId());
		}

		this.invalidate();

		return super.onTouchEvent(motionEvent);

	}

	public void setChooseTabId(int chooseTabId) {
		this.chooseTabId = chooseTabId;
	}

	public void addTabMember(MyTabMember tabMember) {
		mTabMembers.add(tabMember);

	}

	public void setOnTabClickListener(OnTabClickListener onTabClickListener) {
		mOnTabClickListener = onTabClickListener;
	}

	//		
	//	 

	public static interface OnTabClickListener {
		public abstract void onTabClick(int tabId);
	}

}
